package conversion.service.impl;

import conversion.bean.User;
import conversion.service.UserService;
import conversion.vo.SecurityUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SecurityUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.findUserByEmail(username);

        if (null == user){
            log.info("用户不存在");
        }

        String role = user.getRole().toString();
        List<SimpleGrantedAuthority> authorityList = new ArrayList<>();
        authorityList.add(new SimpleGrantedAuthority(role));
        SecurityUser securityUser = new SecurityUser(user);
        securityUser.setAuthorityList(authorityList);
        log.info(securityUser.getUser().toString());

        return securityUser;

    }
}
